package com.zte.cl.task;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zte.cl.model.Img;
import com.zte.cl.model.News;
import com.zte.cl.parser.utils.FileUtil;
import com.zte.cl.repository.ImgRepository;
import com.zte.cl.repository.NewsRepository;
import com.zte.cl.repository.OrigWebpageRepository;
import com.zte.cl.service.NewsService;
import com.zte.cl.utils.ConstantUtil;
import com.zte.cl.utils.DateUtil;

@Service
@Transactional(rollbackFor = {Exception.class})
public class DeleteTask {
	
	private static final Log log = LogFactory.getLog(DeleteTask.class);
	
	@Resource
	private NewsService newsService;
	
	@Resource
	private OrigWebpageRepository origWebpageRepository;
	
	@Resource
	private NewsRepository newsRepository;
	
	@Resource
	private ImgRepository imgRepository;

	/**
	 * 删除抓取失败的数据，每天凌晨1点
	 */
	@Scheduled(cron = "0 0 1 * * ?")
	public void deleteFailData() {
		log.info("开始执行删除抓取失败数据的定时任务...");
		
		// 获取内容为空的原始网页的pageId
		List<String> pageIdList = origWebpageRepository.findPageIdByContentIsNull();
		
		if(pageIdList != null && pageIdList.size() > 0) {
			deleteDataByPageIdList(pageIdList);
		} else {
			log.info("没有需要删除的数据");
		}
		
		log.info("定时删除抓取失败数据执行结束...");
	}
	
	/**
	 * 删除历史数据，每天凌晨2点
	 */
	@Scheduled(cron = "0 0 2 * * ?")
	public void deleteOldData() {
		log.info("开始执行删除历史数据的定时任务...");
		Date date = DateUtil.parseDate(DateUtil.format(DateUtil.AddDay(ConstantUtil.NEWS_DELETE_DAYS_BEFORE)));
		List<String> pageIdList = origWebpageRepository.findPageIdByInsertTimeBefore(date);
		if(pageIdList != null && pageIdList.size() > 0) {
			deleteDataByPageIdList(pageIdList);
		} else {
			log.info("没有需要删除的数据");
		}
		log.info("定时删除历史数据执行结束...");
	}
	
	private void deleteDataByPageIdList(List<String> pageIdList) {
		log.debug("需要删除" + pageIdList.size() +"条原始网页数据");
		
		List<News> newsList = newsRepository.findByPageIdIn(pageIdList);
		List<Img> imgList = null;
		
		if(newsList != null && newsList.size() > 0) {
			log.debug("需要删除" + newsList.size() +"条新闻数据");
			
			List<String> newsIdList = new ArrayList<String>();
			for(News news : newsList) {
				newsIdList.add(news.getId());
			}
			
			imgList = imgRepository.findByNewsIdIn(newsIdList);
			if(imgList != null && imgList.size() > 0) {
				log.debug("需要删除" + imgList.size() +"条图片数据");
				
				// 删除数据库中的img数据
				imgRepository.deleteInBatch(imgList);
			}
			
			// 删除news数据(通过service删除, 同时删除缓存)
			newsService.deleteNews(newsList);
		}
		
		// 删除orig数据
		origWebpageRepository.deleteByPageIdIn(pageIdList);
		
		// 删除硬盘上的图片文件
		if(imgList != null && imgList.size() > 0) {
			for(Img img : imgList) {
				String fullPath = ConstantUtil.IMG_PATH + img.getPath();
				log.debug("删除文件:" + fullPath);
				FileUtil.deleteFile(new File(fullPath));
			}
		}
	}
}
